<?php
class string_library{
    function stringchange($str)
   {
		$str = strip_tags($str);
		$str = mb_strtolower($str,'utf-8');
		$str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", 'a', $str);  //|á|à|ả|ã|ạ|ấ|ầ|ẩ|ẫ|ậ|ắ|ằ|ẳ|ẵ|ặ
		$str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $str);  
		$str = preg_replace("/(ì|í|ị|ỉ|ĩ)/", 'i', $str);  
		$str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", 'o', $str);  
		$str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $str);  
		$str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", 'y', $str);  
		$str = preg_replace("/(đ)/", 'd', $str);
		$str = preg_replace("/(:|;)/","",$str);
		$str = preg_replace("/(́|̀|̉|̃|̣)/","",$str);
		$str = str_replace("_","",$str);
		$str = str_replace("?","",$str);
		$str = str_replace('"',"",$str); 
		$str = str_replace(")","",$str); 
		$str = str_replace("/","",$str);
		$str = str_replace("%","",$str);
		$str = str_replace("&","",$str);
		$str = str_replace("–","",$str);
		$str = str_replace("_","",$str);
		$str = str_replace(".","",$str);
		$str = str_replace(",","",$str);
		$str = str_replace("-","",$str);
		$str = str_replace("(","",$str);
		$str = str_replace("!","",$str); 
		$str = str_replace(" ","-",$str); 
		$str = str_replace("----","-",$str);
		$str = str_replace("---","-",$str);
		$str = str_replace("--","-",$str);
		$str = preg_replace(array('/[^a-zA-Z0-9 -]/'), array(''), $str) ;
		return $str; 

    }        
    
    function changeImgTamtay($value){
        $value = str_replace("http://img.tamtay.vn/files/", "", $value); 
        $value = str_replace("_resize.", "_thumb.", $value); 
        $value = str_replace(".", "_thumb.", $value); 
        $value = str_replace("_thumb_thumb", "_thumb", $value); 
        return $value;
    }

    
    // Hàm chuyen ngay
function chuyenngay($ngayhientai,$songay){           
    $my_time = strtotime ($ngayhientai); //converts date string to UNIX timestamp
    $timestamp = $my_time + ($songay * 86400); //calculates # of days passed ($num_days) * # seconds in a day (86400)
    $return_date = date("Y-m-d H:i:s",$timestamp);  //puts the UNIX timestamp back into string format              
    return $return_date;//exit function and return string
}
    
    // Ham cat chu
function catchu($value, $length){   
        if($value!=''){
        if(is_array($value)) list($string, $match_to) = $value;
        else { $string = $value; $match_to = $value{0}; }

        $match_start = stristr($string, $match_to);
        $match_compute = strlen($string) - strlen($match_start);

        if (strlen($string) > $length)
        {
            if ($match_compute < ($length - strlen($match_to)))
            {
                $pre_string = substr($string, 0, $length);
                $pos_end = strrpos($pre_string, " ");
                if($pos_end === false) $string = $pre_string."...";
                else $string = substr($pre_string, 0, $pos_end)."...";
            }
            else if ($match_compute > (strlen($string) - ($length - strlen($match_to))))
            {
                $pre_string = substr($string, (strlen($string) - ($length - strlen($match_to))));
                $pos_start = strpos($pre_string, " ");
                $string = "...".substr($pre_string, $pos_start);
                if($pos_start === false) $string = "...".$pre_string;
                else $string = "...".substr($pre_string, $pos_start);
            }
            else
            {       
                $pre_string = substr($string, ($match_compute - round(($length / 3))), $length);
                $pos_start = strpos($pre_string, " "); $pos_end = strrpos($pre_string, " ");
                $string = "...".substr($pre_string, $pos_start, $pos_end)."...";
                if($pos_start === false && $pos_end === false) $string = "...".$pre_string."...";
                else $string = "...".substr($pre_string, $pos_start, $pos_end)."...";
            }

            $match_start = stristr($string, $match_to);
            $match_compute = strlen($string) - strlen($match_start);
        }
        
        return $string;
        }else{
            return $string ='';
        } 
}
    // Hàm dinh dang ngay
function format_date($datetime, $format){
    return date($format, $this->mysqldatetime_to_timestamp($datetime));
}

function mysqldatetime_to_timestamp($datetime){
      // function is only applicable for valid MySQL DATETIME (19 characters) and DATE (10 characters)
      $l = strlen($datetime);
        if(!($l == 10 || $l == 19))
          return 0;

        //
        $date = $datetime;
        $hours = 0;
        $minutes = 0;
        $seconds = 0;

        // DATETIME only
        if($l == 19)
        {
          list($date, $time) = explode(" ", $datetime);
          list($hours, $minutes, $seconds) = explode(":", $time);
        }

        list($year, $month, $day) = explode("-", $date);

        return mktime($hours, $minutes, $seconds, $month, $day, $year);
}
    /************Loc chu tim kiem****************/
function getkeyword($text){
           $output = array();
           $output2 = array();
           $arr = explode('"',$text);

           for ($i=0;$i<count($arr);$i++)
           {
               if ($i%2==0)
               {
                $output=array_merge($output,explode(" ",$arr[$i]));
               }
               else $output[] = $arr[$i];
           }
           foreach($output as $word) if (trim($word)!="") $output2[]=$word;
           return $output2;
}
function getOptionDays( $value=1) {
    $r = range(1, 31);
    $str='';
    foreach ($r as $day){
        $str .= "<option value=\"$day\"".($value==$day? ' selected="selected" ':'').">".$day."</option>\n";
    }
    return $str;
}
function getOptionMonth($value=1){
    $str='';
     for ($i = 1; $i <= 12; $i++)
     {
         if($i<10)
             $str .= "<option value=\"0$i\"".($value==$i? ' selected="selected" ':'');
         else
            $str .= "<option value=\"$i\"".($value==$i? ' selected="selected" ':'');
        //$month = date("F", mktime(0, 0, 0, $i+1, 0, 0, 0));
        $str .= '>'.$i.'</option>';
     }

     return $str;
}
 function getOptionYear($start_year, $end_year, $value=1)
    {
		$r = range($start_year, $end_year);
		$str='';
        foreach( $r as $year ) {
            $str .= "<option value=\"$year\"".($value==$year? ' selected="selected" ':'').">".$year."</option>\n";
        }
        return $str;
    }


} 